package com.godyao.mall.pms.service.impl;

import cn.hutool.core.util.StrUtil;
import com.godyao.mall.pms.entity.CategoryBrandRelationEntity;
import com.godyao.mall.pms.service.CategoryBrandRelationService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.godyao.mall.core.utils.PageUtils;
import com.godyao.mall.core.utils.Query;

import com.godyao.mall.pms.dao.BrandDao;
import com.godyao.mall.pms.entity.BrandEntity;
import com.godyao.mall.pms.service.BrandService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;


@Service("brandService")
@RequiredArgsConstructor
public class BrandServiceImpl extends ServiceImpl<BrandDao, BrandEntity> implements BrandService {
    private final CategoryBrandRelationService categoryBrandRelationService;

    @Transactional(rollbackFor = Exception.class)
    @Override
    public void updateCascade(BrandEntity brand) {
        this.updateById(brand);
        categoryBrandRelationService.lambdaUpdate()
                .set(CategoryBrandRelationEntity::getBrandName, brand.getName())
                .eq(CategoryBrandRelationEntity::getBrandId, brand.getBrandId())
                .update();
    }

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        final String key = (String) params.get("key");
        final boolean notBlank = StrUtil.isNotBlank(key);
        //IPage<BrandEntity> page = this.page(
        //        new Query<BrandEntity>().getPage(params),
        //        new QueryWrapper<BrandEntity>()
        //                .like(notBlank, "name", key)
        //                .or()
        //                .like(notBlank, "descript", key)
        //);
        final IPage<BrandEntity> page = this.lambdaQuery().like(notBlank, BrandEntity::getName, key)
                .or()
                .like(notBlank, BrandEntity::getDescript, key)
                .page(new Query<BrandEntity>().getPage(params));
        return new PageUtils(page);
    }

}
